package com.eqdd.yiqidian.ui.discover.workdiary

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.bigkoo.pickerview.TimePickerView
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.kotlin.KotlinBaseActivity
import com.eqdd.yiqidian.ui.base.EditTextActivity
import com.eqdd.yiqidian.utils.DateUtil
import com.eqdd.yiqidian.widget.ChooseAlertView
import kotlinx.android.synthetic.main.activity_work_diary_item.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivityForResult
import org.jetbrains.anko.toast
import java.util.*
import javax.xml.transform.Result

/**
 * 新增、修改日志条目
 * Created by jhj on 18-4-24.
 */
class WorkDiaryItemActivity : KotlinBaseActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        setContentView(R.layout.activity_work_diary_item)
        super.onCreate(savedInstanceState)


        val type = intent.getIntExtra(Config.TYPE, -1)
        initView()

        if (type == -1) {
            finish()
            return
        }

        val title = if (type == 0) {
            "今日计划事项"
        } else {
            "明日临时事项"
        }


        initTopBar(title, "确定", View.OnClickListener {
            if (!isNotNull(type)) {
                return@OnClickListener
            }
            val intent = Intent()
            val pos = getIntent().getIntExtra(Config.POS, -1)
            intent.putExtra(Config.START_TIME, tv_start_time.text.toString())
            intent.putExtra(Config.END_TIME, tv_end_time.text.toString())
            intent.putExtra(Config.CONTENT, tv_plan.text.toString())
            intent.putExtra(Config.RESULT, tv_result.text.toString())
            intent.putExtra(Config.POS, pos)
            setResult(Activity.RESULT_OK, intent)
            finish()
        })

        tv_start_time.onClick {
            initTime(tv_start_time)
        }

        tv_end_time.onClick {
            initTime(tv_end_time)
        }

        tv_plan.onClick {
            if (tv_plan.text.isNullOrEmpty()) {
                startActivityForResult<EditTextActivity>(1000, "title" to title,
                        "tip" to title)
            } else {
                startActivityForResult<EditTextActivity>(1000, "title" to title,
                        "tip" to title, "content" to tv_plan.text.toString())
            }

        }

        if (type == 0) {
            tv_result.onClick {
                initLimit(tv_result)
            }
            tv_plan_label.text = "今日临时事项 :  "
            layout_result.visibility = View.VISIBLE
        } else {
            tv_plan_label.text = "明日计划事项 :  "
            layout_result.visibility = View.GONE
        }


    }


    private fun initView() {
        val startTime = intent.getStringExtra(Config.START_TIME)
        val endTime = intent.getStringExtra(Config.END_TIME)
        val content = intent.getStringExtra(Config.CONTENT)
        val result = intent.getStringExtra(Config.RESULT)

        startTime?.let {
            tv_start_time.text = it
        }
        endTime?.let {
            tv_end_time.text = it
        }
        content?.let {
            tv_plan.text = it
        }
        result?.let {
            tv_result.text = it
        }
    }

    /**
     * 判空
     */
    private fun isNotNull(type: Int): Boolean {
        if (tv_start_time.text.isNullOrEmpty()) {
            toast("开始时间不能为空")
            return false
        }
        if (tv_end_time.text.isNullOrEmpty()) {
            toast("结束时间不能为空")
            return false
        }

        if (type == 0) {
            if (tv_plan.text.isNullOrEmpty()) {
                toast("今日临时事项不能为空")
                return false
            }
            if (tv_result.text.isNullOrEmpty()) {
                toast("今日完成结果不能为空")
                return false
            }
        } else {
            if (tv_plan.text.isNullOrEmpty()) {
                toast("明日计划事项不能为空")
                return false
            }
        }

        return true
    }

    private val now = Calendar.getInstance()
    private fun initTime(view: TextView) {

        if (!view.text.isNullOrEmpty()) {
            val c = Calendar.getInstance()
            val text = view.text.toString()
            val times = text.split(":")
            c.set(Calendar.HOUR_OF_DAY, times[0].trim().toInt())
            c.set(Calendar.MINUTE, times[1].trim().toInt())
            now.time = c.time
        } else {
            now.time = Date()
        }

        val dialog = TimePickerView.Builder(this) { date, v ->
            val time = DateUtil.parse(date, "HH:mm")
            view.text = time
            now.time = date
        }
                .setOutSideCancelable(true)
                .setType(booleanArrayOf(false, false, false, true, true, false))
                .setDate(now)
                .build()
        dialog.show()
    }

    private fun initLimit(textView: TextView) {
        val array = arrayOf("已完成", "未完成", "取消")
        ChooseAlertView.getBottomChoose(this, null, array) { o, pos ->
            textView.setText(array[pos - 1])
        }.show()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Config.INTENT_CONTENT && requestCode == 1000) {
            tv_plan.text = data?.getStringExtra("content")
        }
    }


}